package com.tulele.vtool.handler;

import com.github.pagehelper.PageInfo;
import com.google.common.collect.Maps;
import com.tulele.vtool.model.BBusinessDetails;
import com.tulele.vtool.model.BBusinessInfo;
import com.tulele.vtool.model.respEntity.RespData;
import com.tulele.vtool.model.respEntity.ReturnCode;
import com.tulele.vtool.service.BBusinessDetailsService;
import com.tulele.vtool.service.BBusinessInfoService;
import io.micrometer.core.instrument.util.StringUtils;
import org.apache.commons.collections.CollectionUtils;
import org.apache.ibatis.annotations.Param;
import org.springframework.web.bind.annotation.*;
import com.tulele.vtool.model.BBusinessShare;
import com.tulele.vtool.service.BBusinessShareService;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 商学院课程分享表(BBusinessShare)表控制层
 *
 * @author makejava
 * @since 2020-10-23 11:21:00
 */
@RestController
@RequestMapping("bBusinessShare")
public class BBusinessShareHandler {
    /**
     * 服务对象
     */
    @Resource
    private BBusinessShareService bBusinessShareService;

    /**
     * 服务对象
     */
    @Resource
    private BBusinessDetailsService bBusinessDetailsService;

    /**
     * 服务对象
     */
    @Resource
    private BBusinessInfoService bBusinessInfoService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("selectOne")
    public BBusinessShare selectOne(Integer id) {
        return this.bBusinessShareService.queryById(id);
    }

    /**
     * 新增数据
     *
     * @param bBusinessShare 实例对象
     * @return 实例对象
     */
    @PostMapping(value = "/insert", produces = "application/json;charset=UTF-8")
    public RespData insert(@RequestHeader("userId") String userId, @RequestBody BBusinessShare bBusinessShare) {

        RespData respData = new RespData();

        if (null == bBusinessShare || StringUtils.isBlank(userId) || null == bBusinessShare.getCourseId()) {
            respData.setCode(ReturnCode.CLIENT_ERROR);
            respData.setMsg("参数异常");
            return respData;
        }

        BBusinessInfo bBusinessInfo = this.bBusinessInfoService.queryById(bBusinessShare.getCourseId());

        if(bBusinessInfo == null){
            respData.setCode(ReturnCode.CLIENT_ERROR);
            respData.setMsg("课程不存在");
            return respData;
        }

        bBusinessShare.setUserId(Integer.valueOf(userId));
        Map<String, Object> resultMap = Maps.newHashMapWithExpectedSize(1);
        resultMap.put("bBusinessShare", this.bBusinessShareService.insert(bBusinessShare));

        respData.setData(resultMap);
        respData.setCode(ReturnCode.NORMAL_SUCC);
        respData.setMsg("新增成功");
        return respData;
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @PostMapping(value = "/deleteById", produces = "application/json;charset=UTF-8")
    public RespData deleteById(@RequestHeader("userId") String userId, @Param("id") Integer id) {

        RespData respData = new RespData();

        if (null == id || StringUtils.isBlank(userId)) {
            respData.setCode(ReturnCode.CLIENT_ERROR);
            respData.setMsg("参数异常");
            return respData;
        }

        Map<String, Object> resultMap = Maps.newHashMapWithExpectedSize(1);
        resultMap.put("boolean", this.bBusinessShareService.deleteById(id));

        respData.setData(resultMap);
        respData.setCode(ReturnCode.NORMAL_SUCC);
        respData.setMsg("删除成功");
        return respData;
    }

    /**
     * 修改数据
     *
     * @param bBusinessShare 实例对象
     * @return 实例对象
     */
    @PostMapping(value = "/update", produces = "application/json;charset=UTF-8")
    public RespData update(@RequestHeader("userId") String userId, @RequestBody BBusinessShare bBusinessShare) {

        RespData respData = new RespData();

        if (null == bBusinessShare || StringUtils.isBlank(userId)) {
            respData.setCode(ReturnCode.CLIENT_ERROR);
            respData.setMsg("参数异常");
            return respData;
        }

        Map<String, Object> resultMap = Maps.newHashMapWithExpectedSize(1);
        resultMap.put("bBusinessShare", this.bBusinessShareService.update(bBusinessShare));

        respData.setData(resultMap);
        respData.setCode(ReturnCode.NORMAL_SUCC);
        respData.setMsg("修改成功");
        return respData;
    }

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @PostMapping(value = "/queryById", produces = "application/json;charset=UTF-8")
    public RespData queryById(@RequestHeader("userId") String userId, @Param("id") Integer id) {

        RespData respData = new RespData();

        if (null == id || StringUtils.isBlank(userId)) {
            respData.setCode(ReturnCode.CLIENT_ERROR);
            respData.setMsg("参数异常");
            return respData;
        }

        Map<String, Object> resultMap = Maps.newHashMapWithExpectedSize(1);
        resultMap.put("bBusinessShare", this.bBusinessShareService.queryById(id));

        respData.setData(resultMap);
        respData.setCode(ReturnCode.NORMAL_SUCC);
        respData.setMsg("查询成功");
        return respData;
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit  查询条数
     * @return 对象列表
     */
    @PostMapping(value = "/queryAllByLimit", produces = "application/json;charset=UTF-8")
    public RespData queryAllByLimit(@RequestHeader("userId") String userId, @Param("offset") Integer offset, @Param("limit") Integer limit) {

        Integer pageNum = offset;
        if (pageNum == null || pageNum.intValue() <= 0) {
            pageNum = 1;
        }

        Integer pageSize = limit;
        if (pageSize == null || pageNum.intValue() <= 0) {
            pageSize = 10;
        }

        RespData respData = new RespData();

        if (StringUtils.isBlank(userId)) {
            respData.setCode(ReturnCode.CLIENT_ERROR);
            respData.setMsg("参数异常");
            return respData;
        }

        Map<String, Object> resultMap = Maps.newHashMapWithExpectedSize(1);
        resultMap.put("bBusinessShareList", this.bBusinessShareService.queryAllByLimit(pageNum, pageSize));

        respData.setData(resultMap);
        respData.setCode(ReturnCode.NORMAL_SUCC);
        respData.setMsg("查询成功");
        return respData;
    }

    /**
     * 查询多条数据
     *
     * @param bBusinessShare 实例对象
     * @return 实例对象
     */
    @PostMapping(value = "/selectAllByLimit", produces = "application/json;charset=UTF-8")
    public RespData selectAllByLimit(@RequestHeader("userId") String userId, @RequestBody BBusinessShare bBusinessShare) {

        Integer pageNum = bBusinessShare.getPageNum();
        if (pageNum == null || pageNum.intValue() <= 0) {
            pageNum = 1;
        }

        Integer pageSize = bBusinessShare.getPageSize();
        if (pageSize == null || pageNum.intValue() <= 0) {
            pageSize = 10;
        }

        RespData respData = new RespData();

        if (StringUtils.isBlank(userId) || bBusinessShare == null) {
            respData.setCode(ReturnCode.CLIENT_ERROR);
            respData.setMsg("参数异常");
            return respData;
        }

        // 如果没有userId，就是查询自己的分享课程。
        if (bBusinessShare.getUserId() == null) {
            bBusinessShare.setUserId(Integer.valueOf(userId));
        }

        PageInfo<BBusinessShare> pageInfo = this.bBusinessShareService.selectAllByLimit(pageNum, pageSize, bBusinessShare);

        if (null == pageInfo) {
            respData.setCode(ReturnCode.CLIENT_ERROR);
            respData.setMsg("暂无数据");
            return respData;
        }
        List<BBusinessShare> queryAllByLimitDto = pageInfo.getList();
        if (CollectionUtils.isNotEmpty(queryAllByLimitDto)) {

            List<BBusinessDetails> collect = queryAllByLimitDto.stream().map(info -> {
                BBusinessDetails e = this.bBusinessDetailsService.queryById(info.getCourseId());

                BBusinessDetails temporary = new BBusinessDetails();
                temporary.setBusinessId(e.getBusinessId());
                temporary.setTitle(e.getTitle());
                temporary.setContent(e.getContent());
                temporary.setLookCount(e.getLookCount());
                temporary.setPath(e.getPath());
                temporary.setUpdateTime(info.getUpdateTime());

                return temporary;
            }).collect(Collectors.toList());

            Map<String, Object> resultMap = Maps.newHashMapWithExpectedSize(2);
            resultMap.put("msg", pageInfo.isIsLastPage());
            resultMap.put("collect", collect);

            respData.setData(resultMap);
        }

        respData.setCode(ReturnCode.NORMAL_SUCC);
        respData.setMsg("查询成功");
        return respData;
    }

}